/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-04-02 17:33:24
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-09-12 15:52:34
 * @FilePath: /XMate-h5/src/utils/urlTool.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { isPC } from "./platformTool";

function openUrl(url) {
  // sessionStorage.setItem("newsUrl", url);
  window.open(url, "_blank");
}

function previewFileUrl(fileUrl) {
  console.log("fileUrl", fileUrl);
}

// 网页地址 根目录
function baseWebUrl() {
  return import.meta.env.VITE_XMATE_BASE_URL;
}

// 登录页面
function loginWebUrl() {
  return import.meta.env.VITE_XMATE_LOGIN_URL;
}

function openMeetingRecord(conferenceId) {
  let baseUrl = baseWebUrl();
  if (baseUrl === "https://x-mate.com") {
    // 兼容 不加wwww纪要有问题
    baseUrl = "https://www.x-mate.com";
  }
  if (isPC()) {
    openUrl(`${baseUrl}/#/postmeet/record/${conferenceId}`);
  } else {
    openPathInNewTab(`/record/${conferenceId}`,{},true);
  }
}

function openPathInNewTab(path, query , hiddenAppBar = false) {
  const { origin, pathname } = window.location;
  let url = `${origin}${pathname}#${path}`;
  if (query) {
    url += `?${Object.entries(query).map(([key, value]) => `${key}=${value}`).join("&")}`;
  }
  openInNewTab(url, hiddenAppBar);
}

function openInNewTab(url, hiddenAppBar = false) {

  if (isPC()) {
    window.open(url, "_blank");
    return;
  }

  try {
  window.flutter_inappwebview.callHandler("openNewTab", {
      url,
      hiddenAppBar: hiddenAppBar ? 1 : 0
    });
  } catch (error) {
    window.open(url, "_blank");
  }
} 

export { openUrl, openMeetingRecord, previewFileUrl, baseWebUrl, loginWebUrl, openInNewTab, openPathInNewTab };
